from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse, Http404
from django.shortcuts import render
import datetime


def homepage(request):
    """首页"""
    return HttpResponse('这里是首页')


def current_time(request):
    """获取当前时间"""
    now = datetime.datetime.now()
    # t = Template("<html><body>It is now {{ now }}</body></html>")
    # t = get_template('current_time.html')
    # html = t.render({'current_date': now})
    # return HttpResponse(html)
    return render(request, 'current_time.html', {'current_date': now, 'title': '测试标题'})


def head_hours(request, offset):
    """获取当前时间之后多少小时的时间点"""
    try:
        offset = int(offset)
    except ValueError:
        raise Http404
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    return render(request, 'includes/head_hours.html', {'offset': offset, 'dt': dt, 'title': '小时之后的时间'})


def display_meta(request):
    values = request.META.items()
    values = sorted(values)
    html = []
    for k, v in values:
        html.append('<tr><td>%s</td><td>%s</td></tr>' % (k, v))
    return HttpResponse('<table>%s</table>' % '\n'.join(html))




